﻿using RobotPratolSolution.ViewModels;
using System.Windows;
using System.Windows.Controls;

namespace RobotPratolSolution.Pages
{
    /// <summary>
    /// TaskStationManage.xaml 的交互逻辑
    /// </summary>
    public partial class TaskStationManage : UserControl
    {
        TaskStationListViewModel _TaskStationListViewModel { get; set; }
        public TaskStationManage()
        {
            _TaskStationListViewModel = new TaskStationListViewModel();
            this.DataContext = _TaskStationListViewModel;
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            _TaskStationListViewModel._SelectedJobId = string.Empty;
            _TaskStationListViewModel.JobListInfoRefresh();
            TaskList.ItemsSource = _TaskStationListViewModel.JobInfoModels;
            _TaskStationListViewModel.StationInfoRefresh();
            StationListBox.ItemsSource = _TaskStationListViewModel.StationModels;
            _TaskStationListViewModel.TaskListDetailListRefresh();
            TaskStationListBox.ItemsSource = _TaskStationListViewModel._RobotTaskCtrls;
            _TaskStationListViewModel.LoadRobotInfo();
        }

        private void AddDeviceCtrl_Click(object sender, RoutedEventArgs e)
        {
            int jobIndex = TaskList.SelectedIndex;
            if (jobIndex < 0) return;
            if (AddMove.IsChecked == true && StationListBox.SelectedIndex >= 0)
            {
                StationModelView stationModelView = _TaskStationListViewModel.StationModels[StationListBox.SelectedIndex];
                _TaskStationListViewModel.addMoveCtrl(stationModelView, 0);
            }
            if (AddMoveXY.IsChecked == true && StationListBox.SelectedIndex >= 0)
            {
                StationModelView stationModelView = _TaskStationListViewModel.StationModels[StationListBox.SelectedIndex];
                _TaskStationListViewModel.addMoveCtrl(stationModelView, 1);
            }

            if (AddMoveAngle.IsChecked == true && StationListBox.SelectedIndex >= 0)
            {
                StationModelView stationModelView = _TaskStationListViewModel.StationModels[StationListBox.SelectedIndex];
                _TaskStationListViewModel.addMoveCtrl(stationModelView, 2);
            }
            if (AddMoveXYInLine.IsChecked == true && StationListBox.SelectedIndex >= 0)
            {
                StationModelView stationModelView = _TaskStationListViewModel.StationModels[StationListBox.SelectedIndex];
                _TaskStationListViewModel.addMoveCtrl(stationModelView, 4);
            }
            if (AdvanceDistance.IsChecked == true)
            {
                //StationModelView stationModelView = _TaskStationListViewModel.StationModels[StationListBox.SelectedIndex];
                _TaskStationListViewModel.addMoveCtrl(null, 5);
            }

            if (AlignWithRack.IsChecked == true)
            {
                StationModelView stationModelView = _TaskStationListViewModel.StationModels[StationListBox.SelectedIndex];
                _TaskStationListViewModel.addMoveCtrl(stationModelView, 6);
            }
            if (ToUnloadPoint.IsChecked == true)
            {
                StationModelView stationModelView = _TaskStationListViewModel.StationModels[StationListBox.SelectedIndex];
                _TaskStationListViewModel.addMoveCtrl(stationModelView, 7);
            }

            if (JackUp.IsChecked == true)
            {
                _TaskStationListViewModel.AddJackCtrl(true);
            }
            if (JackDown.IsChecked == true)
            {
                _TaskStationListViewModel.AddJackCtrl(false);
            }





            if (DoCharge.IsChecked == true)
            {
                _TaskStationListViewModel.AddRobotDoCharge();
            }
            if (AddSpinMotrorStart.IsChecked == true)
            {
                _TaskStationListViewModel.AddSpinMotror("on");
            }
            if (AddSpinMotrorEnd.IsChecked == true)
            {
                _TaskStationListViewModel.AddSpinMotror("off");
            }
            if (MotrorUp.IsChecked == true)
            {
                _TaskStationListViewModel.AddMotror("up");
            }
            if (Motrordown.IsChecked == true)
            {
                _TaskStationListViewModel.AddMotror("down");
            }
            if (MotrorStop.IsChecked == true)
            {
                _TaskStationListViewModel.AddMotror("stop");
            }
            if (RedLightOpen.IsChecked == true)
            {
                _TaskStationListViewModel.AddRedLigtOpen("3");
            }

        }

        private void SaveDeviceCtrl_Click(object sender, RoutedEventArgs e)
        {

        }

        private void DeleteDeviceCtrl_Click(object sender, RoutedEventArgs e)
        {
            int index = TaskStationListBox.SelectedIndex;
            if (index >= 0)
            {
                _TaskStationListViewModel.DeleteItem(index);
            }

        }

        private void TaskList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = TaskList.SelectedIndex;
            if (index >= 0)
            {

                _TaskStationListViewModel._SelectedJobId = _TaskStationListViewModel.JobInfoModels[index].Id;

                _TaskStationListViewModel.TaskListDetailListRefresh();
            }
        }

        private void UpActionCtrl_Click(object sender, RoutedEventArgs e)
        {
            var selectindex = TaskStationListBox.SelectedIndex;
            if (selectindex <= 0)
                return;
            _TaskStationListViewModel.UpJobNodeFunc(selectindex);
            TaskStationListBox.SelectedIndex = selectindex - 1;


        }

        private void DownActionCtrl_Click(object sender, RoutedEventArgs e)
        {
            var selectindex = TaskStationListBox.SelectedIndex;
            if (selectindex >= _TaskStationListViewModel._RobotTaskCtrls.Count - 1)
                return;
            _TaskStationListViewModel.DownJobNodeFunc(selectindex);
            TaskStationListBox.SelectedIndex = selectindex + 1;
        }
    }
}
